package com.winning.zjjh.convert;

import org.apache.commons.lang.StringUtils;

import javax.xml.bind.annotation.adapters.XmlAdapter;
import java.text.FieldPosition;
import java.text.NumberFormat;
import java.util.Objects;

public class NumberFormatAdapter extends XmlAdapter<String,Number> {

    private NumberFormat numberFormat = NumberFormat.getNumberInstance();

    @Override
    public Number unmarshal(String v) throws Exception {
        if (StringUtils.isNotEmpty(v)) {
            return numberFormat.parse(v);
        }
        return null;
    }

    @Override
    public String marshal(Number v) throws Exception {
        StringBuffer buffer = new StringBuffer();
        if (Objects.nonNull(v)) {
            numberFormat.format(v, buffer, new FieldPosition(NumberFormat.INTEGER_FIELD));
        }
        System.out.println(buffer.toString());
        return buffer.toString();
    }
}
